package com.beilang.hotline.module.page;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.codelookup.model.CodeModel;
import com.beilang.base.core.page.BasePage;
import com.beilang.base.exception.CheckException;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.property.AppDefaultProperty;
import com.beilang.base.util.CodeConstant;
import com.beilang.base.util.DateUtil;
import com.beilang.hotline.module.manager.ModuleMgr;
import com.beilang.hotline.module.model.ModuleModel;
import com.beilang.hotline.module.to.SearchTO;
import com.beilang.hotline.user.model.UserModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("unchecked")
public class ModulePage extends BasePage{
	private ModuleMgr moduleMgr;
	private CodeLookupMgr codeLookupMgr;
	
	public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
		this.codeLookupMgr = codeLookupMgr;
	}

	public void setModuleMgr(ModuleMgr moduleMgr) {
		this.moduleMgr = moduleMgr;
	}
	
	public List getSysList(){
		List list=new ArrayList();
		list.add(this.getEmptyIntCodeModel());
		list.addAll(getAddSysList());
		return list;
	}

	public List getAddSysList(){
        return codeLookupMgr.getCodeListWithFilter(CodeConstant.SYS_NAME, CodeConstant.CODE_FILTER, new ArrayList());
	}

	public List getUpdateSysList(){
        return this.getAddSysList();
	}    

    public PaginationHelper getSearchList(PaginationHelper helper,SearchTO searchTO){
		helper=moduleMgr.getModuleList(helper, searchTO);
		return helper;
	}	
	
	public PaginationHelper deleteModuleList(PaginationHelper helper,SearchTO searchTO,List deleteList){
        for (Object obj : deleteList) {
            ModuleModel moduleModel = (ModuleModel) obj;
            moduleMgr.deleteModule(moduleModel.getModuleId());
        }
		return this.getSearchList(helper, searchTO);
	}
	
	public ModuleModel addModule(ModuleModel moduleModel) throws CheckException{
		Long sysId=moduleModel.getSysId();
		List param=new ArrayList();
		param.add(sysId);
		List list=codeLookupMgr.getCodeListWithFilter(CodeConstant.MODULE_LIST, CodeConstant.CODE_FILTER, param);
		if(list!=null&&list.size()>0){
            for (Object obj : list) {
                CodeModel codeModel = (CodeModel) obj;
                if (codeModel.getCodeDesc().equalsIgnoreCase(moduleModel.getModuleName())) {
                    throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST));
                }
            }
		}
		UserModel user=this.getLoginUser();
		moduleModel.setCreateId(user.getUserId());
		moduleModel.setUpdateId(user.getUserId());
		Date date = DateUtil.getDate();
		moduleModel.setCreateTime(date);
		moduleModel.setUpdateTime(date);
		moduleModel=moduleMgr.createModule(moduleModel);
		return moduleModel;
	}
	
	public ModuleModel getViewModule (Long moduleId){
		return moduleMgr.getModule(moduleId);
	}
	
	public ModuleModel getUpdateModule (Long moduleId){
		return getViewModule(moduleId);
	}	
	
	public ModuleModel UpdateModule(ModuleModel moduleModel) throws CheckException{
		Long sysId=moduleModel.getSysId();
		List param=new ArrayList();
		param.add(sysId);
		List list=codeLookupMgr.getCodeListWithFilter(CodeConstant.MODULE_LIST, CodeConstant.CODE_FILTER, param);
		if(list!=null&&list.size()>0){
            for (Object obj : list) {
                CodeModel codeModel = (CodeModel) obj;
                if (codeModel.getCodeDesc().equalsIgnoreCase(moduleModel.getModuleName())) {
                    if (!codeModel.getCodeId().equalsIgnoreCase(String.valueOf(moduleModel.getModuleId().longValue())))
                        throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST));
                }
            }
		}		
		UserModel user=this.getLoginUser();
		moduleModel.setUpdateId(user.getUserId());
		moduleModel.setUpdateTime(DateUtil.getDate());	
		moduleModel=moduleMgr.saveModule(moduleModel);
		return moduleModel;
	}
}
